package org.example.filter;

import cn.hutool.core.util.StrUtil;
import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import org.example.utlis.ResponseUtil;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限过滤
 * @author joey
 */
@Slf4j
@Order(1)
@WebFilter(filterName = "oauthFilter", urlPatterns = "/game/*")
public class OauthFilter implements Filter {
    @Resource
    Cache<String, Object> caffeineCacheAuth;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse rep = (HttpServletResponse) response;
        String token = req.getHeader("token");
//        if(StrUtil.isEmpty(token)){
//            ResponseUtil.handle(rep,ResponseUtil.noAuthority3());
//            return;
//        }
//        Object obj = caffeineCacheAuth.getIfPresent(token);
//        if(obj==null){
//            ResponseUtil.handle(rep,ResponseUtil.noAuthority3());
//            return;
//        }
        chain.doFilter(req,rep);
    }

    @Override
    public void destroy() {

    }
}
